﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using gt.dotnetcore.configuration.Options;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Options;

namespace gt.dotnetcore.configuration.Controllers
{
    /*
     * 约定：
     * 键不区分大小写，如果存在相同的键，则取最后一个值
     * 分层键 : 取子节点。或者__双下划线
     * 值默认为字符串
     * 不存储NULL值
     * 
     * */
    [Route("api/[controller]")]
    [ApiController]
    public class ConfigController : ControllerBase
    {
        private IConfiguration _configuration;
        private readonly List<BookOptions> _books;
        private readonly List<BookOptions> _booksNamed;
        private readonly People _people;

        public ConfigController(IConfiguration configuration,
            IOptionsMonitor<List<BookOptions>> bookAccessor,
            IOptionsSnapshot<List<BookOptions>> bookNamedAccessor)
        {
            _configuration = configuration;
            _books = bookAccessor.CurrentValue;
            _booksNamed = bookNamedAccessor.Value;

            var user = new People();
            _configuration.GetSection("People").Bind(user);
            _people = user;
            //_people = _configuration.GetSection("people").Get<People>();
        }
        //读取节点value
        [Route("one")]
        [HttpGet]
        public IActionResult One()
        {
            var result = _configuration.GetValue<string>("one");
            return new OkObjectResult(result);
        }

        //读取子节点value
        [Route("two")]
        [HttpGet]
        public IActionResult Two()
        {
            //var result = _configuration.GetValue<string>("two:_two");
            var result = _configuration.GetSection("two").GetChildren().First(x => string.Equals("_two", x.Key, StringComparison.CurrentCultureIgnoreCase));
            return new OkObjectResult(result.Value);
        }

        //读取自定义配置文件
        [Route("three")]
        [HttpGet]
        public IActionResult Three()
        {
            var result = _configuration.GetValue<string>("three");
            return new OkObjectResult(result);
        }

        //读取数组,注意不能直接getvalue
        [Route("fourToSeven")]
        [HttpGet]
        public IActionResult FourToSeven()
        {
            var result = _configuration.GetSection("fourToSeven").Get<string[]>();
            return new OkObjectResult(result);
        }

        //json绑定到对象
        [Route("people")]
        [HttpGet]
        public IActionResult People()
        {
            return new OkObjectResult(_people);
        }

        //option 注入的方式 
        //注 中文乱码 需要修改 .json文件为utf8格式
        [Route("books")]
        [HttpGet]
        public IActionResult Books()
        {
            return new OkObjectResult(_books);
        }
        //option 注入的方式 
        //注 中文乱码 需要修改 .json文件为utf8格式
        [Route("books2")]
        [HttpGet]
        public IActionResult Books2()
        {
            return new OkObjectResult(_booksNamed);
        }

    }
}